package com.wfm.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
 * Z 字形变换
 * 2025-03-13 15:47:39
 * 时间复杂度 O(N)
 * 空间复杂度 O(N)
 */
class ZigzagConversion {

    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public String convert(String s, int numRows) {
        if(numRows<2) return s;
        List<StringBuilder> rows = new ArrayList<>();
        for(int i=0;i<numRows;i++) rows.add(new StringBuilder());

        int i = 0, flag = -1;
        for(char c:s.toCharArray()){
            rows.get(i).append(c);
            if(i==0 || i==numRows-1) flag = -flag;
            i += flag;
        }
        StringBuilder res = new StringBuilder();
        for(StringBuilder row:rows) res.append(row);
        return res.toString();
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) {
        Solution solution = new ZigzagConversion().new Solution();
        System.out.println(solution.convert("PAYPALISHIRING", 3));
        
    }
}